package com.scetop.bank;

public class CheckingAccount extends Account {

    private double overdraftProtection = -1;
    private SavingsAccount protectedBy;

    public CheckingAccount(double balance) {
        super(balance);

    }

    public CheckingAccount(double balance, SavingsAccount protectedBy) {
        super(balance);
//        overdraftProtection = protect;
        this.protectedBy = protectedBy;
    }

    public boolean Withdraw(double amt) {
        if (amt <= balance) {
            balance -= amt;

        }else {

//            if (overdraftProtection == -1){
//                throw new OverdraftException("资金不足", (amt - balance));
//            }

//            if (overdraftProtection >= (amt - balance)) {
//                balance = 0;
//                overdraftProtection -= (amt - balance);
//            }

            if (protectedBy != null
                    && protectedBy.getBalance() >= (amt - balance)) {
                balance = 0;
                protectedBy.Withdraw (amt - balance);
            }else {

//                throw new OverdraftException("资金不足", (amt - balance));
                return false;
            }
        }
        return true;

    }
}
